<template>
  <div>
      <el-form :model="store" :rules="rules" ref="storeForm">
        <el-form-item label="店铺名称" prop="Sname">
          <el-input v-model="store.Sname" placeholder="请输入店铺名称"></el-input>
        </el-form-item>
        <el-form-item label="店铺销量" prop="sales">
          <el-input v-model.number="store.sales" type="number" :readonly="true" placeholder="请输入店铺销量"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="addStore">确 定</el-button>
      </span>
  </div>

</template>

<script>
import axios from 'axios'; // 导入 axios

export default {
  name: 'Add',
  props: {
    dialogVisible: Boolean,
  },
  data() {
    return {
      store: {
        Sname: '', // 店铺名称
        sales: 0 // 店铺销量
      },
      rules: {
        Sname: [
          { required: true, message: '请输入店铺名称', trigger: 'blur' }
        ],
        sales: [
          { required: true, message: '请输入店铺销量', trigger: 'blur' },
          { type: 'number', message: '销量必须为数字', trigger: 'change' }
        ]
      }
    };
  },
  methods: {
    // 取消操作
    cancel() {
      this.$refs.storeForm.resetFields(); // 重置表单
      this.$emit('add:dialogVisible', false); // 关闭对话框
    },

    // 添加店铺
    addStore() {
      this.$refs.storeForm.validate((valid) => {
        if (valid) {
          console.log('店铺信息:', this.store);

          // 调用 API 添加店铺
          axios.post('http://localhost:21371/api/Store', this.store)
            .then(() => {
              // 添加成功，关闭对话框并通知父组件刷新数据
              this.$emit('update:dialogVisible', false);
              this.$emit('refresh');
            })
            .catch((error) => {
              console.error('添加店铺失败:', error);
            });
        } else {
          console.log('验证失败!');
          return false;
        }
      });
    }
  }
};
</script>

<style scoped>
.el-form-item {
  margin-bottom: 20px;
}
</style>
